<template>
  <div class="edit">
    <div class="top">
      <p>{{ txtVal }}</p>
    </div>
    <div class="bottom">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item prop="name" label="名称:">
          <el-input v-model="form.name" size="medium" style="width: 300px" />
        </el-form-item>
        <el-form-item prop="score" label="积分:">
          <el-input v-model="form.score" size="medium" style="width: 100px" />
          <span class="yuan">分</span>
        </el-form-item>
        <el-form-item prop="stock_count" label="库存:">
          <el-input v-model="form.stock_count" size="medium" style="width: 100px" />
          <span class="yuan">件</span>
        </el-form-item>
        <el-form-item label="选择门店:" prop="cost">
          <el-radio v-model="form.shop_id" label="1">欧丽洛雅吴中路商场</el-radio>
          <el-radio v-model="form.shop_id" label="2">全球家居1号店</el-radio>
        </el-form-item>
        <el-form-item label="配送方式:" prop="cost">
          <el-radio v-model="form.delivery_type" label="1">到店自提</el-radio>
          <el-radio v-model="form.delivery_type" label="2">快递配送</el-radio>
          <el-radio v-model="form.delivery_type" label="3">自提+快递配送</el-radio>
        </el-form-item>
        <el-form-item prop="cover" label="封面图:">
          <el-upload
            action="http://ddsc.izhixiu.cn/api/common/upload"
            :file-list="coverFileList"
            list-type="picture-card"
            :on-preview="handleCoverPreview"
            :on-remove="handleCoverRemove"
            :on-success="hanldeCoverSuccess"
            name="photo"
            :limit="1"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <p style="font-size:12px;color:#7B7B7B;">建议尺寸：750*750px</p>
          <el-dialog :visible.sync="coverDialogVisible">
            <img width="100%" :src="coverDialogImageUrl" alt />
          </el-dialog>
        </el-form-item>
        <el-form-item prop="banners" label="轮播图:">
          <el-upload
            action="http://ddsc.izhixiu.cn/api/common/upload"
            name="photo"
            :file-list="carouselFileList"
            list-type="picture-card"
            :on-preview="handleCarouselPreview"
            :on-remove="handleCarouselRemove"
            :on-success="hanldeCarouselSuccess"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <p style="font-size:12px;color:#7B7B7B;">建议尺寸：750*750px</p>
          <el-dialog :visible.sync="carouselDialogVisible">
            <img width="100%" :src="carouselDialogImageUrl" alt />
          </el-dialog>
        </el-form-item>
        <el-form-item prop="must" label="须知:">
          <el-input
            v-model="form.must"
            type="textarea"
            size="medium"
            style="width: 300px"
            placeholder="例如：积分商品不可退换货"
          />
        </el-form-item>
        <el-form-item prop="detail" label="详情设置">
          <div style="width: 1000px;">
            <Tinymce v-model="form.detail" />
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="save">
      <button class="submit" @click="submit">保存</button>
    </div>
  </div>
</template>
<script>
import Tinymce from "@/components/Tinymce";
import { editIntegralGoods, getIntegralList } from "@/api/http/integral";
const ERR_OK = 200;
export default {
  components: {
    Tinymce
  },
  data() {
    return {
      txtVal: "添加商品",
      id: 0,
      coverFileList: [],
      carouselFileList: [],
      link: "",
      form: {
        name: "",
        score: "",
        stock_count: "",
        shop_id: "1",
        delivery_type: "1",
        cover: "",
        banners: [],
        must: "",
        detail: ""
      },
      value1: [],
      coverDialogVisible: false,
      coverDialogImageUrl: "",
      carouselDialogVisible: false,
      carouselDialogImageUrl: "",
      activeName: 1,
      radio: "1",
      radio1: "1",
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        score: [{ required: true, message: "请输入积分", trigger: "blur" }],
        stock_count: [
          { required: true, message: "请输入库存", trigger: "blur" }
        ],
        cover: [{ required: true, message: "请上传封面图", trigger: "change" }],
        banners: [
          { required: true, message: "请上传轮播图", trigger: "change" }
        ],
        must: [{ required: true, message: "请填写购买须知", trigger: "blur" }],
        detail: [{ required: true, message: "请填写购买须知", trigger: "blur" }]
      },
      goodsInfo: null
    };
  },
  created() {
    if (this.$route.query.detail) {
      this.txtVal = "编辑商品";
      this.goodsInfo = this.$route.query.detail;
      this.form.name = this.goodsInfo.name;
      this.form.score = this.goodsInfo.score;
      this.form.stock_count = this.goodsInfo.stock_count;
      this.form.shop_id = this.goodsInfo.shop_id + "";
      this.form.delivery_type = this.goodsInfo.delivery_type + "";
      this.form.cover = this.goodsInfo.cover;
      this.coverFileList = [{ name: this.form.cover, url: this.form.cover }];
      this.form.banners = this.goodsInfo.banners;
      this.goodsInfo.banners.forEach((item, index) => {
        this.carouselFileList.push({ name: item, url: item });
      });
      this.form.must = this.goodsInfo.must;
      this.form.detail = this.goodsInfo.detail;
    }
  },
  methods: {
    submit() {
      if (this.$route.query.detail) {
        this.form.id = this.$route.query.detail.id;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          this._editIntegralGoods(this.form).then(res => {
            if (res.code == 200) {
              this.$message({
                message: "操作成功",
                showClose: true,
                type: "success"
              });
              this.$router.back();
            }
          });
        }
      });
    },
    handleCoverPreview(file) {
      this.coverDialogImageUrl = file.url;
      this.coverDialogVisible = true;
    },
    handleCoverRemove(file, fileList) {
      this.coverDialogImageUrl = "";
      this.form.cover = "";
    },
    hanldeCoverSuccess(response, file, fileList) {
      if (response.code === ERR_OK) {
        this.form.cover = response.data.photo;
      }
    },
    handleCarouselPreview(file) {
      this.carouselDialogImageUrl = file.url;
      this.carouselDialogVisible = true;
    },
    handleCarouselRemove(file) {
      const fIndex = this.form.banners.findIndex(item => {
        return item === file.url;
      });
      if (fIndex >= 0) {
        this.form.banners.splice(fIndex, 1);
      }
      this.carouselDialogImageUrl = "";
    },
    hanldeCarouselSuccess(response, file, fileList) {
      if (response.code === ERR_OK) {
        this.form.banners.push(response.data.photo);
      }
    },
    handleChange(index) {
      this.radio = index;
      this.activeName = index;
    },
    _editIntegralGoods({
      name,
      score,
      stock_count,
      shop_id,
      cover,
      banners,
      must,
      detail,
      id,
      delivery_type
    }) {
      return editIntegralGoods({
        name,
        score,
        stock_count,
        shop_id,
        cover,
        banners,
        must,
        detail,
        id,
        delivery_type
      })
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res);
            return Promise.resolve(res);
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>
<style lang="scss" scoped>
@import "@/styles/el-button.scss";
.yuan {
  position: absolute;
  top: 0;
  right: 7px;
  color: #101010 !important;
}
.el-form /deep/ .el-form-item__content {
  // width: 500px;
}
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  p {
    line-height: 44px;
    color: rgba(68, 68, 68, 1);
    font-size: 18px;
    text-align: left;
    font-family: Roboto;
    font-weight: bold;
  }
}
.bottom {
  .shop-wrapper {
    margin-top: 40px;
    margin-left: 10px;
    .row {
      display: flex;
      align-items: center;
      margin-top: 10px;
      .title {
        margin-right: 10px;
      }
      ul {
        display: flex;
        align-items: center;
        margin: 0;
        padding: 0;
        li {
          list-style: none;
          margin-right: 10px;
          span {
            line-height: 23px;
            color: rgba(102, 102, 102, 1);
            font-size: 14px;
            text-align: center;
            font-family: Roboto;
          }
        }
      }
    }
  }
}
.save {
  .submit {
    display: block;
    margin: 0 auto;
    width: 200px;
    line-height: 48px;
    border-radius: 6px;
    background-color: rgba(43, 51, 62, 1);
    text-align: center;
    font-size: 16px;
    color: #fff;
    border: none;
    outline: none;
  }
}
.mt {
  margin-top: 30px;
}
</style>
